<?php

namespace app\api\controller;

use app\common\controller\Api;
use fast\Random;
use think\Config;
use think\Db;

/**
 * 分享接口
 */
class Share extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }

    /**
     * 分享状态
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="status", type="string", required=true, sample="返回成功",description="状态:1=可分享,2=不可分享")
     */
    public function getShareStatus()
    {
        //返回可分享 1
        $result['status'] = 1;

        /*
        $share_log = DB::name("share_log")->where(array("user_id"=>$this->auth->id))->order("num desc")->find();
        $result = array();
        if(empty($share_log)){
            $result['status'] = 1;
        }else{
            $num = $share_log['num']/5;
            if(is_int($num)){
                if($num > 1){
                    $cha = ($num-1)*5+1;
                    $find_share = DB::name("share_log")->where(array("num"=>$cha,"user_id"=>$this->auth->id))->find();
                    $where = "createtime >= ".$find_share['time']." and pid =".$this->auth->id;

                    $share_notify = DB::name("share_notify")->where($where)->group("user_id")->select();
                    $is = 1;
                    if($share_notify){
                        foreach($share_notify as $key => $val){
                            // $new_where = "createtime < ".$find_share['time']." and pid = ".$find_share['user_id']." and user_id =".$val['user_id'];
                            $new_where = "createtime < ".$find_share['time']." and user_id =".$val['user_id'];
                            $new_share_notify = DB::name("share_notify")->where($new_where)->find();
                            if(empty($new_share_notify)){
                                $is = 2;
                            }
                        }
                        if($is == 1){
                            $result['status'] = 2;
                        }else{
                            $result['status'] = 1;
                        }
                    }else{
                        $result['status'] = 2;
                    }
                }else{
                    $share_notify = DB::name("share_notify")->where('pid',$this->auth->id)->group("user_id")->select();
                    if($share_notify){    
                        $result['status'] = 1;
                    }else{
                        $result['status'] = 2;
                    }
                }
            }else{
                $result['status'] = 1;
            }
        }

        */
        $this->success('查询成功', $result);
    }

    /**
     * 添加分享
     * @ApiMethod (POST)
     * @param string $url  分享地址
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doAddShareLog()
    {
        /**
         * 5月9号最新需求
        1.用户第一次分享微信小程序时立刻送1K嘶豆(无论有没有人点击都送这1K)，只有在首次分享的时候送，后续无论分享几次都不送嘶豆
        2.点击分享链接进来的用户如果是未注册的新用户，那么分享的那个人再得1K嘶豆，但是进来的新用户不送任何嘶豆
        3.用户在进入小程序以后，首页不再有领1W嘶豆的功能了，默认嘶豆数就是0，但是可以看昨夜故事，到摸与不摸的地方弹出充值面板付费
         */
        $url = input("url");
        $share_log = DB::name("share_log")->where(array("user_id"=>$this->auth->id))->order("num desc")->find();

        if(empty($share_log)){
            //从来没有分享过
            $num = 1;
        }else {
            //已经分享过
            $num = $share_log['num']+1;
        }

        //插入分享记录
        $data = array(
            "user_id" => $this->auth->id,
            "num" => $num,
            "url" => $url,
            "time" => time(),
            "create_time" => date("Y-m-d H:i:s")
        );
        DB::name("share_log")->insert($data);

        //得豆逻辑
        if($num == 1){
            $user_find = DB::name("user")->where("id",$this->auth->id)->find();
            //更新嘶豆
            DB::name("user")->where("id",$this->auth->id)->setInc("money",1000);
            $money_log = array(
                "user_id" => $this->auth->id,
                "money" => 1000,
                "before" => $user_find['money'],
                "after" => $user_find['money']+1000,
                "memo" => "首次分享获取1000嘶豆",
                "createtime" => time()
            );
            DB::name("user_money_log")->insert($money_log);

            $result = [
                'is_change_money' => 1,//1-有变化
            ];
            $this->success("分享成功",$result);

        }else {
            $result = [
                'is_change_money' => 2,//2-没有变化
            ];
            $this->success("分享成功",$result);
        }

        //判断这个人当天分享了几次，如果小于2次，就+500
        /*$url = input("url");
        $share_log = DB::name("share_log")->where(array("user_id"=>$this->auth->id))->order("num desc")->find();

        if(empty($share_log)){
            $num = 1;
        }else {
            $num = $share_log['num']+1;
        }

        //插入分享记录
        $data = array(
            "user_id" => $this->auth->id,
            "num" => $num,
            "url" => $url,
            "time" => time(),
            "create_time" => date("Y-m-d H:i:s")
        );
        DB::name("share_log")->insert($data);

        $count = DB::name("share_log")->where(array("user_id"=>$this->auth->id))->where('create_time','>=',date('Y-m-d 00:00:00'))->where('create_time','<=',date('Y-m-d 23:59:59'))->count();
        if($count <= 2){
            $user_find = DB::name("user")->where("id",$this->auth->id)->find();
            //更新嘶豆
            DB::name("user")->where("id",$this->auth->id)->setInc("money",500);
            $money_log = array(
                "user_id" => $this->auth->id,
                "money" => 500,
                "before" => $user_find['money'],
                "after" => $user_find['money']+500,
                "memo" => "分享获取500嘶豆",
                "createtime" => time()
            );
            DB::name("user_money_log")->insert($money_log);

            $result = [
                'is_change_money' => 1,//1-有变化
            ];
            $this->success("分享成功",$result);

        }else {
            $result = [
                'is_change_money' => 2,//2-没有变化
            ];
            $this->success("分享成功",$result);
        }
        */



        /*
        $url = input("url");
        $share_log = DB::name("share_log")->where(array("user_id"=>$this->auth->id))->order("num desc")->find();
        if(empty($share_log)){
            $num = 1;
        }else{
            $num = $share_log['num']+1;
        }
        //插入分享记录
        $data = array(
            "user_id" => $this->auth->id,
            "num" => $num,
            "url" => $url,
            "time" => time(),
            "create_time" => date("Y-m-d H:i:s")
        );
        DB::name("share_log")->insert($data);
        $user_find = DB::name("user")->where("id",$this->auth->id)->find();
        //更新嘶豆
        DB::name("user")->where("id",$this->auth->id)->setInc("money",1000);
        $money_log = array(
            "user_id" => $this->auth->id,
            "money" => 1000,
            "before" => $user_find['money'],
            "after" => $user_find['money']+1000,
            "memo" => "分享获取1000嘶豆",
            "createtime" => time()
        );
        DB::name("user_money_log")->insert($money_log);
        $this->success("分享成功");
        */
    }
}
